package dao

import (
	"mall-portalv2/internal/model"
)

func (d Dao) UpdateOmsOrderReturnApplyAndOrder(returnId, orderId int64, returnUpdates, orderUpdates map[string]interface{}) error {
	tb1 := model.OmsOrderReturnApply{}
	tb2 := model.OmsOrder{}

	tx := d.db.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()

	if err := tx.Error; err != nil {
		return err
	}

	if err := tx.Table(tb1.TableName()).Where("id = ?", returnId).Updates(returnUpdates).Error; err != nil {
		tx.Rollback()
		return err
	}

	if err := tx.Table(tb2.TableName()).Where("id = ?", orderId).Updates(orderUpdates).Error; err != nil {
		tx.Rollback()
		return err
	}

	return tx.Commit().Error
}
